package com.ocgs.dao.impl;

import com.ocgs.base.core.exception.BaseDAOException;
import com.ocgs.base.util.StringUtil;
import com.ocgs.dao.StorageDAO;
import com.ocgs.mapper.StorageRecordMapper;
import com.ocgs.model.Product;
import com.ocgs.model.StorageRecord;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: bryant
 * Date: 10/21/12
 * Time: 3:40 PM
 * To change this template use File | Settings | File Templates.
 */
public class StorageDAOImpl implements StorageDAO {

    private SqlSessionTemplate sqlSessionTemplate;

    public SqlSessionTemplate getSqlSessionTemplate() {
        return sqlSessionTemplate;
    }

    public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
        this.sqlSessionTemplate = sqlSessionTemplate;
    }


    public int deleteByPrimaryKey(Integer id) throws BaseDAOException {

        try {
            return sqlSessionTemplate.getMapper(StorageRecordMapper.class).deleteByPrimaryKey(id);
        } catch (Exception e) {
            throw new BaseDAOException(e);
        }
    }


    public int insert(StorageRecord record) throws BaseDAOException {
        try {
            return sqlSessionTemplate.getMapper(StorageRecordMapper.class).insert(record);
        } catch (Exception e) {
            throw new BaseDAOException(e);
        }

    }

    public int insertSelective(StorageRecord record) throws BaseDAOException {
        try {
            return sqlSessionTemplate.getMapper(StorageRecordMapper.class).insert(record);
        } catch (Exception e) {
            throw new BaseDAOException(e);
        }
    }


    public StorageRecord selectByPrimaryKey(Integer id) throws BaseDAOException {
        try {
            return sqlSessionTemplate.getMapper(StorageRecordMapper.class).selectByPrimaryKey(id);
        } catch (Exception e) {
            throw new BaseDAOException(e);
        }
    }


    public int updateByPrimaryKeySelective(StorageRecord record) throws BaseDAOException {
        try {
            return sqlSessionTemplate.getMapper(StorageRecordMapper.class).updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            throw new BaseDAOException(e);
        }
    }


    public int updateByPrimaryKey(StorageRecord record) throws BaseDAOException {
        try {
            return sqlSessionTemplate.getMapper(StorageRecordMapper.class).updateByPrimaryKey(record);
        } catch (Exception e) {
            throw new BaseDAOException(e);
        }
    }

    public List<StorageRecord> findAllInOutStorage(Product product) throws BaseDAOException {
        try {
            if (product != null) {
                Product search = new Product();
                search.setProductName(StringUtil.escapeSQLString(product.getProductName()));
                search.setProductNo(StringUtil.escapeSQLString(product.getProductNo()));
                search.setBrandId(product.getBrandId());
                search.setCategoryId(product.getCategoryId());
                return sqlSessionTemplate.getMapper(StorageRecordMapper.class).findAllInOutStorage(search);
            } else return null;

        } catch (Exception e) {
            throw new BaseDAOException(e);
        }
    }
}
